//
//  joueur.c
//  BatailleNavaleRoyale
//
//  Created by Amer Nasser on 15/11/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#include <stdio.h>
#include <stdlib.h>
#include "joueur.h"
#include <string.h>



coordonne * createCoordonne(int x,int y){
    
    coordonne *c=malloc(sizeof(coordonne));
    
    c->x=x;
    c->y=y;
    
    return c;
}


joueur * createPlayer(char *username){
    
    joueur * j=malloc(sizeof(joueur));
    
    j->username=username;
    
    // createPlayerSubs(j->subs);
    j->subs[0]=createSubMarine(1);
    j->subs[1]=createSubMarine(1);
    j->subs[2]=createSubMarine(2);
    j->subs[3]=createSubMarine(2);
    j->subs[4]=createSubMarine(3);
    j->subs[5]=createSubMarine(3);
    j->subs[6]=createSubMarine(4);
    
    j->x=0;
    j->y=0;
    
    int i,k;
    for(i=0;i<16;i++){
        for(k=0;k<16;k++){
            j->positions[i][k]=0;
        }
    }
    
    return j;
}


subMarine * createSubMarine(int partie){
    
    subMarine *s=malloc(sizeof(subMarine));
    
    s->partie=partie;
    
    s->partieTouche=0;
    
    s->xy=malloc(partie * sizeof(coordonne*));
    s->xyTouche=malloc(partie *sizeof(coordonne*));
    
    return s;
    
    
}

void movePlayer (joueur * player , int x,int y){
    (player->x)+=x;
    (player->y)+=y;
}

void attaquePlayer (joueur *player ){
    player->positions[player->x][player->y]=1;
}

void touchPlayer (joueur *player){
    player->positions[player->x][player->y]=2;
}


void partOfSubDestroyed(subMarine* subs[], int absicce,int ordonne ,int taille){
    
    int i,j;
    subMarine *sub;
    for(i=0;i<taille;i++){
        sub=subs[i];
        for(j=0;j<sub->partie;j++){
            if(subs[i]->xy[j]->x==absicce  && subs[i]->xy[j]->y==ordonne){
                subs[i]->xyTouche[sub->partieTouche]=createCoordonne(absicce,ordonne);
                (sub->partieTouche)++;
            }
        }
    }
    
}



